package tests;

import static org.junit.Assert.*;

import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Vector;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import MyLinkedList.MyLinkedList;

public class IteratorTests {
	private MyLinkedList<Integer> _l = null;
	
	@Before
	public void setUp() {
		_l = new MyLinkedList<Integer>();
	}
	
	@After
	public void tearDown() {
		_l = null;
	}

	@Test (expected=IllegalStateException.class)
	public void RemoveThrowsException() {
		int N = 20;
		Vector<Integer> elems = new Vector<Integer>();
		for(int i = 0; i < N; ++i)
			elems.add(i);
		
		int sizeVector = elems.size();
		for(int i = 0; i < sizeVector; ++i)
			_l.add(i, elems.elementAt(i));
		
		Iterator<Integer> it = _l.iterator();
		
		
		while (it.hasNext()) {
			it.next();
			it.remove();
			it.remove();
		}
	}
	
	@Test
	public void IterateOverAList() {
		int N = 20;
		Vector<Integer> elems = new Vector<Integer>();
		for(int i = 0; i < N; ++i)
			elems.add(i);
		
		int sizeVector = elems.size();
		for(int i = 0; i < sizeVector; ++i)
			_l.add(i, elems.elementAt(i));
		
		int i = 0;
		for (Integer element : _l)
			assertTrue(element.equals(elems.elementAt(i++)));
	}
	
	@Test (expected=ConcurrentModificationException.class)
	public void IterateListThrowsConcurrentModificationException() {
		int N = 20;
		Vector<Integer> elems = new Vector<Integer>();
		for(int i = 0; i < N; ++i)
			elems.add(i);
		
		int sizeVector = elems.size();
		for(int i = 0; i < sizeVector; ++i)
			_l.add(i, elems.elementAt(i));
		
		
		
		for (@SuppressWarnings("unused") Integer element : _l) {
			_l.remove(_l.size()-1);
		}
	}

}
